package usefulUtils;

import java.lang.reflect.Array;
import java.util.Arrays;

/**
 * @author le
 */
public class CommonUsefulUtils {
    /**
     * java的常用的工具
     * 文件：zip/rar/tar解压缩、文件夹、xml、excel表格、普通文本（txt）
     */

    public static void main(String[] args) {

        // 待排序的数组
        int[] nums = {1, 56, 8, 9, 65, 88,10};
        CommonUsefulUtils usefulUtils = new CommonUsefulUtils();
        int[] arr = usefulUtils.sort(nums);
        for (int i = 0; i < arr.length; i++)
        {
            System.out.println(arr[i]);
        }

    }

    public int[] sort(int[] source){
        int[] arr = Arrays.copyOf(source,source.length);

        if (arr.length < 2){
            return arr;
        }
        int mid = (int)Math.floor(arr.length / 2);

        int[] left = Arrays.copyOfRange(arr,0,mid);
        int[] right = Arrays.copyOfRange(arr,mid,arr.length);

        return merge(sort(left),sort(right));
    }

    public int[] merge(int[] left, int[] right){
        int[] res = new int[left.length + right.length];
        int i = 0;
        while(left.length > 0 && right.length > 0){
            if (left[0] > right[0]){
                res[i++] = right[0];
                right = Arrays.copyOfRange(right,1,right.length);
            }else{
                res[i++] = left[0];
                left = Arrays.copyOfRange(left,1 ,left.length);
            }
        }

        while(left.length > 0){
            res[i++] = left[0];
            left = Arrays.copyOfRange(left,1 ,left.length);
        }

        while(right.length > 0){
            res[i++] = right[0];
            right = Arrays.copyOfRange(right,1 ,right.length);
        }
        return res;
    }

}
